import {
  Toast
} from 'mint-ui'

export function trim (str) {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}
export function copyToClipboard (text) {
  if (text.indexOf('-') !== -1) {
    let arr = text.split('-')
    text = arr[0] + arr[1]
  }
  var textArea = document.createElement('textarea')
  textArea.style.position = 'fixed'
  textArea.style.top = '0'
  textArea.style.left = '0'
  textArea.style.width = '2em'
  textArea.style.height = '2em'
  textArea.style.padding = '0'
  textArea.style.border = 'none'
  textArea.style.outline = 'none'
  textArea.style.boxShadow = 'none'
  textArea.style.background = 'transparent'
  textArea.value = text
  document.body.appendChild(textArea)
  textArea.select()

  try {
    var successful = document.execCommand('copy')
    var msg = successful ? '成功复制到剪贴板' : '该浏览器不支持点击复制到剪贴板'
    Toast(msg || '操作成功')
  } catch (err) {
    Toast(err || '该浏览器不支持点击复制到剪贴板')
  }

  document.body.removeChild(textArea)
}
export function getTimes (date) {
  if (!date) {
    return '----'
  } else {
    var d = new Date(date)
    var year = d.getFullYear()
    var month = ('0' + (d.getMonth() + 1)).slice(-2)
    var day = ('0' + d.getDate()).slice(-2)
    var hour = ('0' + d.getHours()).slice(-2)
    var minutes = ('0' + d.getMinutes()).slice(-2)
    var seconds = ('0' + d.getSeconds()).slice(-2)
    return year + month + day + hour + minutes + seconds
  }
}
export function dateFormat (date) {
  if (date === '' || date === undefined) {
    return '----'
  } else {
    var d = new Date(date)
    var year = d.getFullYear()
    var month = ('0' + (d.getMonth() + 1)).slice(-2)
    var day = ('0' + d.getDate()).slice(-2)
    return year + '-' + month + '-' + day
  }
}
// 回到顶部
export function backToTop () {
  window.scrollTo(0, 0)
}

export function getResultData (data, status, msg) {
  const result = {
    'data': {},
    'msg': '',
    'status': 0
  }
  if (data) {
    result.data = data
  }
  if (status) {
    result.status = status
  }
  if (msg) {
    result.msg = msg
  }

  return result
}
